/* 
 * File:   Window.cpp
 * Author: lucia
 * 
 * Created on 31 de agosto de 2014, 17:28
 */

#include "Window.h"

#include <string>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>

using namespace std;

Window::Window() {
    pWindow = SDL_CreateWindow("Snow Bros Suripantas", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
    this->myLogFile->getInstance(MY_LOG_MODE);
}

Window::~Window() {
    //SDL_DestroyWindow(pWindow);
    pWindow = NULL;
}

Window::Window(string name, int w, int l, Uint32 flag, SDL_Renderer** gRenderer) {
    this->myLogFile->getInstance(MY_LOG_MODE);
    string lineToLog = "";
    width = w; //ancho
    length = l; // alto

    if (SDL_Init(SDL_INIT_VIDEO) < 0) {
        string error(SDL_GetError());
        lineToLog = "[2] SDL no se pudo inicializar. Error: " + error + "\n";
        myLogFile->writeInLog(2, lineToLog);
    } else {
        if (!SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "1")) {
            lineToLog = "[2] Linear texture filtering not enabled!\n";
            myLogFile->writeInLog(2, lineToLog);
        } else {
            pWindow = SDL_CreateWindow(name.data(), SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, w, l, flag); // FLAG = SDL_WINDOW_SHOWN
            if (pWindow == NULL) {
                string error(SDL_GetError());
                lineToLog = "[2] La ventana no pudo ser creada. Error: " + error + "\n";
                myLogFile->writeInLog(2, lineToLog);
            } else {
                *gRenderer = SDL_CreateRenderer(pWindow, -1, SDL_RENDERER_ACCELERATED);
                if (*gRenderer == NULL) {
                    
                string error(SDL_GetError());
                lineToLog = "[2] El Renderer no pudo ser creado. Error: " + error + "\n";
                myLogFile->writeInLog(2, lineToLog);
                } else {
                    SDL_SetRenderDrawColor(*gRenderer, 0xFF, 0xFF, 0xFF, 0xFF);
                }

            }
        }
    }
}

bool Window::modifySize(int w, int l) {
    width = w;
    length = l;

    return true;
}

SDL_Window* Window::getWindow() {
    return pWindow;
}

int Window::getLength() {
    return length;
}

int Window::getWidth() {
    return width;
}

bool Window::isInit() {
    if (pWindow == NULL) {
        return false;
    }
    return true;
}

